window.onload = function () {

    let loginWrap = document.querySelector(".login-wrap")
    let inputs = document.querySelectorAll("input")
    let btn = document.querySelector(".btn");
    document.addEventListener("mousemove", () => {
        if (inputs[0].value == `` || inputs[1].value=='') {
            btn.disabled = true
        }
        else if (inputs[0].value != '' && inputs[1].value!='') {
            btn.disabled = false
        }
    }
)
    //点击登录事件
    btn.onclick = function (evt) {
        //登录接口：接口地址：http://jx.xuzhixiang.top/ap/api/login.php

        let username = inputs[0].value;
        let password = inputs[1].value;
        let loginAPI = `http://jx.xuzhixiang.top/ap/api/login.php`
        let p = axios.get(loginAPI, {
            params: {
                username,
                password
            }
        });
        p.then(r => {
            console.log(r.data);
            console.log(r.data.msg);
            if (r.data.code === 0) {
                hidden.style.display = "block"
                hidden.innerHTML =  r.data.msg + "!" 
            } else if (r.data.code === 1) {
                //登陆成功
                //登录的id存入本地
                console.log("成功");
                localStorage.setItem("id", r.data.data.id)
                localStorage.setItem("username", r.data.data.username)
                console.log(r.data.data.username);
                console.log(r.data.data.id);
                alert(r.data.msg);
                //token存入本地
                localStorage.setItem("token", r.data.data.token)
                //token登录凭证，登录的时候返回
                location.href = "index.html"
            }
        })
        evt.preventDefault();
        document.addEventListener("click", () => hidden.style.display = "none")
    }
    inputs[2].onclick = function () {
        location.href = "reg.html"
    }
}